<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\Goods;
use app\admin\model\News;
use app\admin\model\NewsCate;
use app\admin\model\Notice;
use app\admin\model\Nav;
use app\common\controller\Api;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     */
    public function index()
    {
        $this->success('获取成功', [
            'daohang' => $this->nav(), //获取金刚区信息
        ], 1);
    }

    /**
     *首页搜索
     */
    public function search()
    {
        $where = input('title');
        if (empty($where)) {
            $this->error('请输入商品名称！', [], 500);
        }
        $model = new Goods();
        $data = $model->where(['status' => 1, 'type' => 1])
            ->where(function ($query) use ($where) {
                if ($where) {
                    $query->where('title', 'like', '%' . $where . '%');
                }
            })
            ->select();
        foreach ($data as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        $this->success('获取成功', $data, 1);
    }

    /**
     * banner
     *
     */
    public function banner()
    {
        //1=首页,2=商城
        $type = input('type/d', 1);
        $model = new Banner();
        $data = $model->where(['status' => 1, 'type' => $type])
            ->field('id,title,image')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        $this->success('请求成功', $data, 1);
    }

    /**
     * 金刚区
     *
     */
    public function nav()
    {
        $model = new Nav();
        $data = $model->where(['status' => 1])
            ->field('id,title,image')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        return $data;
    }

    /**
     * 公告列表
     *
     */
    public function notice()
    {
        $list = Notice::where(['status' => 1])
            ->field('id,title,image,createtime')
            ->select();
        foreach ($list as &$v) {
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
            if (!empty($v['image'])) {
                $v['image'] = Config('url_domain_root') . $v['image'];
            }
        }
        // 获取列表id
        $id = input('id/d', 1);
        $detail = Notice::where(['status' => 1, 'id' => $id])
            ->field('id,title,content,createtime')
            ->select();
        foreach ($detail as &$v) {
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
//            $v['content'] = strip_tags($v['content']); // 去掉HTML标签
        }
        $this->success('获取成功', $data = ['list' => $list, 'detail' => $detail], 1);
    }

    /**
     * 积分兑换
     *
     */
    public function exchange()
    {
        $type = input('type/d', 1); //1=积分商品，2=拼团商品
        $model = new Goods();
        $data = $model::where(['is_top' => 1, 'type' => $type])
            ->field('id,title,image,score')
            ->limit(0, 3)
            ->select();
        foreach ($data as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        $this->success('获取成功！', $data, 1);
    }

    /**
     * 新闻
     *
     */
    public function news()
    {
        // 新闻分类
        $cates = NewsCate::where(['status' => 1])->field('id,title')->select();
        // 新闻列表
        $cate = input('id/d', 1);
        $list = News::where(['status' => 1, 'news_cate_id' => $cate])
            ->field('id,news_cate_id,title,image,createtime')
            ->limit(0, 4)
            ->select();
        foreach ($list as &$a) {
            $a['createtime'] = date('Y-m-d H:s', $a['createtime']);
            $a['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $a['image']));
        }
        // 新闻详情
        $cate_id = input('cate_id/d');
        $detail = News::where(['status' => 1, 'id' => $cate_id])
            ->field('id,title,image,content,createtime')
            ->find();
        if (!empty($detail)) {
            $detail['createtime'] = date('Y-m-d H:s', $detail['createtime']);
//            $detail['content'] = strip_tags($detail['content']); // 去掉HTML标签
            $detail['content'] = $detail['content']; // 去掉HTML标签
            $detail['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $detail['image']));
        }
        $this->success('获取成功', $data = ['cates' => $cates, 'list' => $list, 'detail' => $detail], 1);
    }

    /**
     * 弹窗消息
     */
    public function alert()
    {
        $notice = !empty(Config('site.alert_notice')) ? Config('site.alert_notice') : '暂无公告！';
        $this->success('获取成功', $notice, 1);
    }

}
